package com.smh.a09ThreadDemo9;

public class MyThread extends Thread {
    //这个类所有对象使用同一个元素
    static int ticket = 0;
    static Object object = new Object();

    @Override
    public void run() {
        //里面要有锁对象(一定要是唯一的)
        while (true) {
            synchronized (MyThread.class) {//同步代码块
                //这里面一般写MyThread.class
                if (ticket == 100) {
                    break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                ticket++;
                System.out.println(getName() + "在卖第" + ticket + "张票");
            }
        }

    }
}
